<?php
ob_start();
session_start();
$flag=0;
?>


<script type="text/javascript"> Initialize_JoptionPane();</script>
<?php
function message_convert_url($string){
  return preg_replace("~(http|https|ftp|ftps)://(.*?)(\s|\n|[,.?!](\s|\n)|$)~", "<a href=\"$1://$2\" target=\"_blank\" rel=\"nofollow\">$1://$2</a>$3", $string);
}

$id=$_SESSION['id'];

if(!isset($_SESSION['offset'])) $_SESSION['offset']=0 ;

$startoffset= $_SESSION['offset']; 

$endoffset= $_SESSION['offset']+21;

$flag=0;

if (isset($_GET['pag'])){
   $pag = $_GET['pag'];  
   $_SESSION['current_page']=$pag;
 
}else{
   if(isset($_SESSION['current_page'])){
       $pag=$_SESSION['current_page'];
   }else{  
       $pag=1;
   }
} 

  try{      
      
     include_once('restconnection.php'); 
     if(isset($client)){  
      
         if(isset($_POST['where_db'])){ 
             $where_db=$_POST['where_db'];
             $where=  str_replace("1","'",$where_db);
         }else{
             $where="";
           
         }
         
        //echo$id;
       
        //retrive base url 
        $url = $_SESSION['basic_url'];
        //add service url
        $service_url="/GetMyTags?";
        //params url
        $params = array('id'=>$id,'where'=>$where,'start_offset'=>$startoffset,'end_offset'=>$endoffset); 
        //params connection
        $ctx = stream_context_create(array('http' => array('timeout' => 25))); 
        //send GET request
        $output = @file_get_contents($url.$service_url."id=".$params['id']."&"."where=".urlencode($params['where'])."&"."start_offset=".$params['start_offset']."&"."end_offset=".$params['end_offset'],0,$ctx);
        //parse JSON output
      // print_r($url.$service_url."id=".$params['id']."&"."where=".$params['where']."&"."start_offset=".$params['start_offset']."&"."endoffset=".$params['end_offset']); break;
        $data = json_decode($output);
        
        $webResult = $data->GetMyTagsResult; 
         
        if($webResult[0]!= null){   
          
          $get_posts=$webResult;
          //numbero of all records
         // print_r($get_posts);
          $num_records=count($get_posts);
         //  print_r($num_records);
       
           if($num_records!=1){
                for($i=0;$i<=15;$i++){
                   if($get_posts[$i]!=null){

                        $split = explode("+-+",$get_posts[$i]);                       
                       
                            $tagger[$i]=$split[0];
                            $logo[$i]=$split[1];

                            $mess[$i]=$split[2];   
                            $message[$i]=message_convert_url($mess[$i]);

                            $datetimeerr[$i]=substr($split[3], 0, -3);
                            $datetime[$i]=str_replace(".",":",$datetimeerr[$i]);

                            $socialnet[$i]=$split[4];
                            $img_message[$i]=$split[5];
                   }
                   
                    
                    
                }//for
                
                if($get_posts[15]!=null){
                     
                    $flag=1; //significa che il 15° elemento è pieno e quindi mostro il bottone
                    
                }//if
            }else{
                
                    $split = explode("+-+",$get_posts);
  
                    $tagger[0]=$split[0];                        
                    $logo[0]=$split[1];
                    $mess[0]=$split[0];   
                    $message[0]=message_convert_url($mess[0]);
                    $datetimeerr[0]=substr($split[3], 0, -3);
                    $datetime[0]=str_replace(".",":",$datetimeerr[0]);
                    $socialnet[0]=$split[4];
                    $img_message[0]=$split[5];  
            }

                ?>
                <div  class="timeline">
                <?php
                 for($i=0;$i<15;$i++){
                
                    if($get_posts[$i]!=null){
        ?>             
                <div class="user_post" id="post<?php echo$i;?>">
                        <div id="avatar_writer" class="user_avatar fl" >
                            <img src="<?php echo$logo[$i]; ?>" title="<?php echo$tagger[$i]; ?>" /> </div> 
                    <div class="other fr">
                        <?php 
                            if($socialnet[$i] == "Facebook") echo"<img class='icon_post_social' src='esa/img/img_user_provenience_posts/fb.png' title='$socialnet[$i]' />";
                            if($socialnet[$i] == "Google")  echo"<img class='icon_post_social'   src='esa/img/img_user_provenience_posts/go.png' title='$socialnet[$i]' />";
                            if($socialnet[$i] == "Twitter") echo"<img class='icon_post_social'  src='esa/img/img_user_provenience_posts/tw.png' title='$socialnet[$i]' />";
                            if($socialnet[$i] == "Pinterest")  echo"";
                            if($socialnet[$i] == "Linkedin")  echo"";
                            if($socialnet[$i] == "Foursquare") echo"";    
                            
                        ?> 
                           
                        <div id="user_name" class="user_name fl"><?php echo$tagger[$i]; ?></div>
                        <div id="user_message_date" class="user_message_date fl" ><?php echo" il $datetime[$i] "; ?></div>
                    </div> 
                        <div class="clear"></div><br />
                        <div id="user_message" class="user_message fl"><?php echo" $message[$i] ";  ?></div><br />
                        <?php if($img_message[$i]!=""){ ?>
                        <img src="<?php echo" $img_message[$i] "; ?>" />
                        <?php }?>
                
                         <div class="clear"></div>
                </div>    
        <?php
                 }//if
              }//for
            ?>
                </div>
            <?php    
          }else{
              ?>
               <label style="margin-left:0px;">There are no posts </label>
               <!--<script type='text/javascript'>ReduceDiv(".scrolltimelines");</script>-->
              <?php  
          }
     }//isset(client)    
     
     if($flag==1){ $flag=0; ?>
         <script>can_download_other_posts('home');</script>
          <div id="otherposts" >
             <label><a title="Load Other Post" >Other Posts</a></label><br />             
             <img id="imgloading" class="hidden"  src="esa/img/loading.gif" />    
             <input type="hidden" value="2" id="numberclick" />
          </div>
    <?php }
            
  }catch (Exception $e){
       
      ?>
          <label style="color:red;font-size:15px;text-align:center">Sorry General Error!</label><br/>
          <label><a><element onclick=" var link='esa/home.php'; ovv_mainother(link);" > Click to continue </a></label>
          
      <?php    
       
   }        
        ?>    
  